from .forms import *
from xadmin.views.base import ModelAdminView

from xadmin import site as adminx_site

# Register your models here.


class StoreAdmin(object):
    fields = ['name', 'store_type']
    list_display = ['name']
adminx_site.register(Store, StoreAdmin)


class BrandAdmin(object):
    fields = ['name', 'logo_public_file_id', 'video_banner_public_file_id', 'attention_account', 'store', 'no']
    list_display = ['name', 'store', 'no']
adminx_site.register(Brand, BrandAdmin)


class ProductAdmin(object):
    form = ProductForm
    list_display = ['name', 'brand', 'tea_type', 'no', 'logo_public_file_id', 'status']
    def save_model(self, request, obj, form, change):
        if change:
            logo_public_file_id = self.put_file_to_aliyun_oss(request.FILES.get('logo_public_file_id', None))
            if logo_public_file_id is not None:
                obj.logo_public_file_id = logo_public_file_id
            video_banner_public_file_id = self.put_file_to_aliyun_oss(request.FILES.get('video_banner_public_file_id', None))
            if video_banner_public_file_id is not None:
                obj.video_banner_public_file_id = video_banner_public_file_id
            obj.save()
        else:
            obj.save()

adminx_site.register(Product, ProductAdmin)


class GoodsBlockAdmin(ModelAdminView):
    form = GoodsBlockForm
    list_display = ['product', 'unique_code', 'store_unit_id', 'no', 'owner_user', 'locked_by_user', 'locked_expire_at_dt_format']

adminx_site.register(GoodsBlock, GoodsBlockAdmin)


class IndexBannerAdmin(object):
    fields = ['banner_public_file_id', 'link']
adminx_site.register(IndexBanner, IndexBannerAdmin)

class ProductLongImageAdmin(object):
    fields = ['product', 'banner_public_file_id', 'order']
    list_display = ['product', 'order']
adminx_site.register(ProductLongImage, ProductLongImageAdmin)


class MarketOrderAdmin(object):
    list_display = ('user', 'product', 'intention_type',
                    'publish_type', 'offered_price', 'count',
                    'direct_to_user', 'as_gift', 'buy_in_market_order',
                    'related_order', 'related_order_status', 'get_add_time', 'status')
adminx_site.register(MarketOrder, MarketOrderAdmin)


class PackageAdmin(object):
    pass
adminx_site.register(Package, PackageAdmin)
